﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GBT.ParamEntity;
using Microsoft.AspNetCore.Mvc;
using NLog;
using PersistenceDataLogic.Core;
using QueryDataLogic.Core;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebAPI_QiSiMealSystem.Controllers
{
    [Route("gbt/[controller]")]
    [ApiController]
    public class AnnounceController : ControllerBase
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        OperationAnnounce operationAnnounce = new OperationAnnounce();
        AnnounceManage announceManage = new AnnounceManage();

        // POST: gbt/Announce/GetAnnounce
        [HttpPost("GetAnnounce")]
        public async Task<IActionResult> GetAnnounce([FromBody] System_Announcement value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                int totalNumber = 0;
                int totalPage = 0;
                string Error = "";
                object result = null;
                try
                {
                    result = announceManage.GetAnnounce(value, ref totalNumber, ref totalPage);
                }
                catch (Exception ex)
                {
                    string number = DateTime.Now.ToString("ddHHmmssfff");
                    Error = "错误代码：" + number + " 详细信息：" + ex.Message + Environment.NewLine + ex.StackTrace;
                    logger.Error(ex, "错误代码：" + number);
                }
                return Ok(new { data = result, Error, totalNumber, totalPage });
            });
            return task;
        }

        // POST: gbt/Announce/OptAnnounce
        [HttpPost("OptAnnounce")]
        public async Task<IActionResult> OptAnnounce([FromBody] System_Announcement value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                string Error = "";
                string msg = "";
                int result = 0;
                string number = "";
                try
                {
                    result = operationAnnounce.OptAnnounce(value, ref msg, ref number);
                }
                catch (Exception ex)
                {
                    Error = "错误代码：" + number + " 详细信息：" + ex.Message + Environment.NewLine + ex.StackTrace;
                    logger.Error(ex, "错误代码：" + number);
                }
                return Ok(new { root = result, msg, Error });
            });
            return task;
        }
    }
}
